﻿using System;
using System.Web.Security;

using NetDon.Utilities;
using SaleDiscount.Portal.Service;
using SaleDiscount.Portal.eBay;
using eBay.Service.Core.Sdk;


namespace SaleDiscount.Portal
{
    public partial class _Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtUserName.Text = "testuser_bweb";

                //txtPassword.Attributes.Add("value", "admin");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim().Length == 0)
            {
                lblMessage.Text = "Please input user name.";
                return;
            }
          
            string userName = txtUserName.Text.Trim();
            var service = new SaleDiscountService();
            var token = service.GetEbayUserToken(userName);
            string message = string.Empty;
            if (token !=null && !string.IsNullOrEmpty(token.Token))
            {
                FormsAuthentication.SetAuthCookie(userName, true);
                Session["CurrentUser"] = userName;
                Session["Status"] = "Active";
                Session["EbayUserTokenId"] = token.Id;
                
                eBaySession.LoadApiContext(token.Token, userName);

                Response.Redirect("~/Default.aspx");
            }
            else
            {
                FetchToken(userName);
                return;
            }
        }

        private void FetchToken(string userName)
        {
            Session["CurrentUser"] = userName;
            Response.Redirect("~/SaleDiscountManagement.aspx");
         
        }
    }
}
